package logic

import (
	"context"
	"fmt"

	"MBTI/rpc/admin/adm"
	"MBTI/rpc/admin/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchStuByClassNoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchStuByClassNoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchStuByClassNoLogic {
	return &SearchStuByClassNoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchStuByClassNoLogic) SearchStuByClassNo(in *adm.SearchStuByClassNoReq) (*adm.SearchResp, error) {
	// todo: add your logic here and delete this line
	fmt.Println("开查")
	_AllStudent, er := l.svcCtx.StudentModel.CtoS(in.ClassNo)
	fmt.Println("用班级号查完了")
	if er != nil {
		return nil, er
	}

	searchHandle := make([]*adm.Search, 0)
	var total int64
	for _, sn := range _AllStudent {
		result, _, err := l.svcCtx.ResultModel.SearchResultByStuNo(sn.StudentNo)
		for _, re := range result {
			searchHandle = append(searchHandle, &adm.Search{
				StudentNo:   re.StudentNo,
				StudentName: re.Student.StudentName,
				AcademyName: re.Student.AcademyName,
				ClassName:   re.Student.ClassName,
				CreateAt:    re.CreatedAt.String(),
				MBTI:        re.MBTI,
				Answer:      re.Answer,
			})
			total++
		}
		if err != nil {
			return nil, err
		}
	}
	return &adm.SearchResp{Total: total, Searchresp: searchHandle}, nil
}
